<?php

class AggregateDefinition extends EntityDefinition implements IAggregateDefinition
{

  /**
   *
   * @var array
   */
  protected $associations = array();

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAggregateDefinition#addAssociation($name, $def)
   */
  public function addAssociation($name, IAssociationDefinition $def)
  {
    $this->associations[$name] = $def;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAggregateDefinition#getAssociation($name)
   */
  public function getAssociation($name)
  {
    if (! $this->hasAssociation($name)) {
      throw new AssociationNotFoundException("Association not found: <b>$name</b>");
    }

    return $this->associations[$name];
  }

  /**
   *
   * @return array
   */
  public function getAssociations()
  {
    return $this->associations;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAggregateDefinition#hasAssociation($name)
   */
  public function hasAssociation($name)
  {
    return isset($this->associations[$name]);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IDomObjDefinition#factory()
   * @return IAggregate
   */
  public function factory()
  {
    $class = $this->class;

    if (! class_exists($class)) {
      return new CustomAggregate($class, $this);
    }

    return parent::factory();
  }

}

?>